package cn.kais.drawer.bean;

import cn.kais.drawer.unit.UnitPicture;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 价签
 * 营销模板中的价签模板
 * 需要先生成价签模板图片流，再将流合并到营销模板上
 */
public class BeanPrice extends BeanBasic {

    public int top;
    public int left;
    public int width;
    public int height;

    public BufferedImage ima;
    public String src = "https://g.csdnimg.cn/common/csdn-footer/images/badge.png"; // 图片地址

    public Graphics2D sd; // 绘制


    public BeanPrice(Graphics2D sd, JSONObject com, BufferedImage ima) {
        this.sd = sd;
        this.ima = ima;
        this.top = com.getInteger("top");
        this.left = com.getInteger("left");
        this.width = com.getInteger("width");
        this.height = com.getInteger("height");

        // 非生产时使用模板信息
        JSONArray list = com.getJSONArray("priceTagList");
        if (list != null && !list.isEmpty()) {
            JSONObject item = list.getJSONObject(0);
            this.src = item.getString("priceTagSrc");
        }

        this.create();
    }

    private void create() {
        if (ima == null) {
            // 营销模板 制作时使用
            BufferedImage ima = UnitPicture.iPictureBuffer(this.src);
            if (ima != null) {
                sd.drawImage(ima, this.left, this.top, this.width, this.height, null);
            }
        } else {
            // 营销模板 生产时使用
            sd.drawImage(this.ima, this.left, this.top, this.width, this.height, null);
        }
//        sd.setColor(Color.RED);
//        BasicStroke stroke = new BasicStroke(this.lineWidth);
//        sd.setStroke(stroke);
//        sd.drawRect(this.left, this.top, this.width, this.height);
    }

}
